package com.hengtuo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hengtuo.base.BaseFragment;
import com.hengtuo.kaiyuanzhongguo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 动弹
 * Created by msi on 2016/5/7.
 */
public class Moveball_Fragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    String[] Title = new String[]{"最新动弹", "热门动弹", "我的动弹"};
    private List<Fragment> list;
    private FragmentPagerAdapter mAdapter;

    //查找布局
    @Override
    protected int getLoadViewId() {
        return R.layout.f2;
    }

    //查找控件
    @Override
    protected void initView(View view) {
        mTabLayout = (TabLayout)view. findViewById(R.id.tl);
        mViewPager = (ViewPager)view.findViewById(R.id.viewpager);
    }

    //主方法
    @Override
    protected void initData() {

        titlebar();

    }

    private void titlebar() {

        addFragment();
        mViewPager.setAdapter(mAdapter);
        //设置tab模式，当前为系统默认模式
        // mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        //设置tabLayout 标题来自mAdapter.getPageTitle
        mTabLayout.setTabsFromPagerAdapter(mAdapter);
        //和ViewPager实现联动滑动
        TabLayout.TabLayoutOnPageChangeListener listener = new TabLayout.TabLayoutOnPageChangeListener(mTabLayout);

        //这里是addOnPageChangeListener,也就是说，你还可以add N个Listener而不会被覆盖掉。
        mViewPager.addOnPageChangeListener(listener);

        mTabLayout.setupWithViewPager(mViewPager);

    }


    private void addFragment() {
        list = new ArrayList<>();
        List<String> liststring=new ArrayList<>();
        Moveball_show_Fragment fragment = null;

        Bundle bundle = null;
        String[] id = new String[]{"0", "-1", "1"};


        for (int i = 0; i < id.length; i++) {
            liststring.add(id[i]);
        }

        for (int i = 0; i < Title.length; i++) {
            fragment=new Moveball_show_Fragment();
            bundle = new Bundle();
            bundle.putString("MOVEBALLUID", liststring.get(i));
            fragment.setArguments(bundle);
            list.add(fragment);
        }

        //此方法用来显示tab上的名字
        mAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            //此方法用来显示tab上的名字
            @Override
            public CharSequence getPageTitle(int position) {

                return Title[position];

            }

            @Override
            public int getCount() {
                return list.size();
            }


            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }
        };


    }

}
